package com.wwdx.service.impl;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.wwdx.model.WwdxBookModel;
import com.wwdx.model.WwdxThemeModel;
import com.wwdx.service.WwdxBookServiceI;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author liguoliang
 */
@Service
public class WwdxBookServiceImpl extends BaseServiceImpl<WwdxBookModel> implements WwdxBookServiceI{

    @Override
    public Long selectModelCount(WwdxBookModel model){
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(6);
        String hql = "select count(1) from WwdxBookModel\t"+getParams(model,params);
        return count(hql,params);
    }

    private String getParams(WwdxBookModel model, Map<String, Object> params){
        StringBuilder sb = new StringBuilder("where 1=1\n");
        String orderBy = "\torder by gmtCreated desc";
        //书籍ID查询
        Long bookid = model.getBookid();
        if (bookid != null){
            sb.append("and bookid=:bookid\n");
            params.put("bookid",bookid);
        }
        //书籍名称查询
        String bookname = model.getBookname();
        if (StringUtils.isNotBlank(bookname)){
            sb.append("and bookname like :bookname\n");
            params.put("bookname","%"+bookname+"%");
        }
        //是否上线查询
        Integer status = model.getStatus();
        if (status != null){
            sb.append("and status=:status\n");
            params.put("status",status);
        }
        //撰稿人或音频转述师下的书籍查询
        Long userid = model.getUserid();
        if (userid != null){
            sb.append("and (videoauthorid=:userid or copywriter=:userid)\n");
            params.put("userid",userid);
        }
        //书籍关键字查询
        String word = model.getWord();
        if (StringUtils.isNotBlank(word)){
            sb.append("and (bookname like :word or bookauthor like :word)\n");
            params.put("word","%"+word+"%");
        }
        //是否首页显示查询
        Integer ishost = model.getIshost();
        if (ishost != null){
            orderBy = "\tORDER BY sort desc";
            params.put("ishost",ishost);
            sb.append("and ishost=:ishost\t");
        }
        //书籍类型查询
        Integer type = model.getType();
        if (type != null){
            params.put("type",type);
            sb.append("and type=:type\t");
        }
        //最热排序查询
        Integer type1 = model.getType1();
        if(type1 != null && type1 == 2){
            orderBy = "\tORDER BY reference_click_count desc";
        }
        //主题下的书籍查询
        Long themeid = model.getThemeid();
        if (themeid != null){
            WwdxThemeModel wwdxThemeModel = wwdxThemeService.getByThemeId(themeid);
            String bookList = wwdxThemeModel.getBook_list();
            Iterable<String> iterable = Splitter.on(",").omitEmptyStrings().trimResults().split(bookList);
            List<Long> bookids = Lists.newArrayListWithExpectedSize(16);
            iterable.forEach(id -> bookids.add(Long.valueOf(id)));
            params.put("ids",bookids);
            sb.append("and bookid IN (:ids)\n");
        }
        //随机获取书籍数查询
        Boolean random = model.isRandom();
        if (random){
            orderBy = "\torder by rand()";
        }
        sb.append(orderBy);
        return sb.toString();
    }

    @Override
    public List<WwdxBookModel> selectModelList(WwdxBookModel model,Integer page,Integer rows){
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(6);
        String hql = "from WwdxBookModel\t"+getParams(model,params);
        return find(hql,params,page,rows);
    }

    @Override
    public void bookStatistics(Long bookid) {
        WwdxBookModel wwdxBookModel = this.getByBookId(bookid);
        Long clickCount = wwdxBookModel.getClick_count();
        Long referenceClickCount = wwdxBookModel.getReference_click_count();
        if (clickCount == null){
            clickCount = 0L;
        }
        if (referenceClickCount == null){
            referenceClickCount = 1000L;
        }
        clickCount ++;
        referenceClickCount += RandomUtils.nextInt(1, 4);
        wwdxBookModel.setClick_count(clickCount);
        wwdxBookModel.setReference_click_count(referenceClickCount);
        wwdxBookModel.setGmtModified(new Date());
        update(wwdxBookModel);
    }

    @Override
    public WwdxBookModel getByBookId(Long bookid){
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(2);
        params.put("bookid",bookid);
        return getByHql("from WwdxBookModel where bookid=:bookid", params);
    }
}